package com.example.demo.controller;

import com.example.demo.Mappers.GetLisSqMapper;
import com.example.demo.beans.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018-10-17.
 */
@RestController
public class GetLisJysqController {
    @Autowired
    private GetLisSqMapper mapper;
    @ResponseBody
    @RequestMapping(value = "GetLisJysq",produces = "application/xml")
    public GetLisJysqResponse getLisjysq(@RequestBody(required = false)Lis_jysqRequest request)
    {
        GetLisJysqResponse response=new GetLisJysqResponse();
        if(request.getCardNo()!=null&&!request.getCardNo().equals(""))
    {
        List<Lis_jysqList> list=mapper.getLisJysq(request.getCardNo(),request.getStartDate(),request.getEndDate());
       response.setResultCode("0");
       response.setResultMsg("返回成功");
       response.setItems(list);
       return response;
    }
    response.setResultCode("1");
     response.setResultMsg("请求为空");
      return  response;
    }

    @RequestMapping(value = "GetLisInfo",produces = "application/xml")
    public LisdjResponse getLisInfo(@RequestBody(required = false)GetLisInfoRequest request)
    {
        LisdjResponse response=new LisdjResponse();
        if(request!=null)
        {
            List<LisdjBean> list=mapper.GetLisInfo(request.getStartDate(),request.getEndDate());
            response.setResultCode("0");
            response.setResultMsg("返回成功");
            response.setLisdjBeans(list);
            return response;
        }
        response.setResultCode("1");
        response.setResultMsg("请求为空");
        return  response;
    }

@ResponseBody
@RequestMapping(value = "GetLismx",produces = "application/xml")
    public Lis_mxResponse getLismx(@RequestBody Lis_jymxRequest request)
    {    Lis_mxResponse response=new Lis_mxResponse();
        if(request.getRepId()!=null&&! request.getRepId().equals(""))
    { List<Lis_mx_TempBean> b=mapper.getTempLis(request.getRepId());
        List<Lis_xzjgBean> ls=mapper.getxzjg();
        Map<String, String>map=new HashMap<String, String>();
        for(int k=0;k<ls.size();k++)
        {map.put(ls.get(k).getBm(),ls.get(k).getMc()); }
        List<Lis_mxBean> beans=new ArrayList<>();
        for(int i=0;i<b.size();i++)
        {
            if(b.get(i).getIsDisplay().equals("0"))
            {     Lis_mxBean be=new Lis_mxBean();
                   be=setLiszero(b.get(i),map);
                   beans.add(be);   }
                   if(b.get(i).getIsDisplay().equals("1"))
                   {  Lis_mxBean be=new Lis_mxBean();
                      be=setLiszone(b.get(i));
                      beans.add(be);
                   }

        }

        response.setResultCode("0");
        response.setResultMsg("返回成功");
        response .setItems(beans);
        return response;
    }
   response.setResultCode("1");
    response.setResultMsg("失败，请检查请求参数，如有问题请您联系检验科核实");
   return  response;
    }


    private Lis_mxBean setLiszero(Lis_mx_TempBean b,Map<String ,String>map)
    {
        Lis_mxBean be=new Lis_mxBean();
        be.setResult(map.get(b.getQualitative()));
        be.setHighLowFlag("不显示");
        be.setIsDisplay(b.getIsDisplay());
        be.setItemCode(b.getItemCode());
        be.setItemName(b.getItemName());
        be.setItemUnit(b.getItemUnit());
        be.setQualitative(b.getQualitative());
        be.setQuantitative(b.getQuantitative());
        be.setReference(b.getReference());
        be.setRefRangeMax(b.getRefRangeMax());
        be.setRefRangeMin(b.getRefRangeMin());
        be.setReserved(b.getReserved());
        return  be;
    }

  private   Lis_mxBean setLiszone(Lis_mx_TempBean b)
    {
        Lis_mxBean be=new Lis_mxBean();
        be.setResult(b.getQuantitative());
        //最小值
        Double d=Double.valueOf(b.getRefRangeMin());
        //最大值
        Double f=Double.valueOf(b.getRefRangeMax());
        //最终结果
        Double j=Double.valueOf(b.getQuantitative());
        if(j-d>=0&&f-j>=0 )
        {be.setHighLowFlag("正常");}
        if(j-d<0)
        {be.setHighLowFlag("偏低");}
        if(f-j<0){
            be.setHighLowFlag("偏高");
        }
        be.setIsDisplay(b.getIsDisplay());
        be.setItemCode(b.getItemCode());
        be.setItemName(b.getItemName());
        be.setItemUnit(b.getItemUnit());
        be.setQualitative(b.getQualitative());
        be.setQuantitative(b.getQuantitative());
        be.setReference(b.getReference());
        be.setRefRangeMax(b.getRefRangeMax());
        be.setRefRangeMin(b.getRefRangeMin());
        be.setReserved(b.getReserved());
        return  be;
    }
}
